<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.beaut.project.util.FlowUtility" %>
<%@ include file="/public/header.jsp"%>

<html>
	<head>
		<title>项目支付结算</title>
	</head>
	<script type="text/javascript">
	var xmStatus = new Array("新建","未开工", "在建", "完工", "验收", "送审", "开票" ,"结算", "关闭"); 
		$(function(){
			$("#searchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		 doSearch();
		       	} 
		   	}); 
			$("#dept_id").combotree({
				url:'<%=contextPath %>/springMVC/group/listDepartment',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					doSearch();
				}
			});
			$("#table").datagrid({
				title:"项目收入结算",
				iconCls:'icon-edit',
				loadMsg:'正在加载数据，请稍后......',
				url:'<%=contextPath %>/springMVC/xm/listPageXMPaymentBalance?statusFlag=1',
				rownumbers:true,
				fitColumns:true,
				nowrap:false,
				idField:'id',
				frozenColumns:[[
				    {title:'主键',field:'xm_id',hidden:true},
				]],
				columns:[[
					{title:'项目进度',field:'xm_progress_status',width:100,align:'center',formatter:function(value,rowData,rowIndex){
					    return xmStatus[value];
					}},
					{title:'所属部门',field:'admin_dept',width:100,align:'center'},
					{title:'项目编号',field:'xm_code',width:100,align:'center'},
					{title:'项目名称',field:'xm_name',width:100,align:'center'},
					{title:'已收金额',field:'has_receivable',width:100,align:'center'},
					{title:'管理费',field:'xm_manage_price',width:100,align:'center'},
					{title:'是否无材料',field:'no_material',hidden:true},
					{title:'分包结算',field:'balance_fb_end_pay',hidden:true},
					{title:'采购结算',field:'balance_cg_end_pay',hidden:true},
					{title:'结算付款金额',field:'balance_end_pay',width:100,align:'center',formatter:function(value,rowData,rowIndex){
						if(rowData.no_material==0){
						if(rowData.balance_fb_end_pay == undefined || rowData.balance_cg_end_pay == undefined){
							return "未结算";
						}else{
							return rowData.balance_fb_end_pay+rowData.balance_cg_end_pay;
						}
						}else{
							return rowData.balance_fb_end_pay;
						}
					}},
					{title:'已付结算金额',field:'balance_already_pay',width:100,align:'center'},
					{title:'支出结算审核状态',field:'xm_payment_balance_status',width:100,align:'center',formatter:function(value,rowData,rowIndex){
						if(value==1){
							return "审核中";
						}else if(value==2){
							return "审核不通过";
						}else if(value==3){
							return "审核通过";
						}
						return "未提交审核";
					}},
					{title:'结算付款操作',field:'cc',width:100,align:'center',formatter:function(value,rowData,rowIndex){
						var str="";
						if(rowData.xm_payment_balance_status== 3){
							str+= "<input type='button' style='border:0px #ff0000 solid;width:16px;height:16px;cursor:pointer;' class='icon-transfer' title='结算付款' onclick=\"javascript:viewXMEndPaymentBalance('"+rowData.xm_id+"','"+rowData.xm_code+"','"+rowData.xm_name+"','"+rowData.customer_id+"','"+rowData.customer+"','"+rowData.admin_dept_id+"','"+rowData.admin_dept+"',"+rowData.xm_business_step+","+rowData.no_material+");\"/>&nbsp;&nbsp;"
						+  "<input type='button' style='border:0px #ff0000 solid;width:16px;height:16px;cursor:pointer;' class='icon-lock' title='结算完成' onclick=\"javascript:finishBalance('"+rowData.xm_code+"');\"/>";
						}
						else
						{
	                    	str+="无";
						}
	                    return str;
					}},
					{title:'操作',field:'operation',width:100,align:'center',formatter:function(value,rowData,rowIndex){
						var str="";
						$.ajax({
							type:'POST',
							async:false,
							url:'<%=contextPath %>/springMVC/xmFinance/hasXMEndPayMain?xm_id='+rowData.xm_id,
							success:function(rtJson){
								if(rtJson == '0'){
									str =  "<input type='button' style='border:0px #ff0000 solid;width:16px;height:16px;cursor:pointer;' class='icon-add' title='结算操作' onclick=\"javascript:addXMPaymentBalance('"+rowData.xm_id+"','"+rowData.xm_code+"','"+rowData.xm_name+"');\"/>";
								}else{
									if(rowData.xm_payment_balance_status ==0||rowData.xm_payment_balance_status==2){
										//还没提交审核时，可以编辑
										str = "<input type='button' style='border:0px #ff0000 solid;width:16px;height:16px;cursor:pointer;' class='icon-add' title='结算操作' onclick=\"javascript:addXMPaymentBalance('"+rowData.xm_id+"','"+rowData.xm_code+"','"+rowData.xm_name+"');\"/>&nbsp;&nbsp;"
												+"<input type='button' style='border:0px #ff0000 solid;width:16px;height:16px;cursor:pointer;' class='icon-shenpi' title='审批' onclick=\"javascript:shenpi(this,'"+rowData.xm_id+"','"+rowData.xm_code+"','"+rowData.xm_name+"');\"/>";	
									}else{
										str = "<input type='button' style='border:0px #ff0000 solid;width:16px;height:16px;cursor:pointer;' class='icon-shenpi' title='审核查看' onclick=\"javascript:JqueryViewFlowView('"+encodeURIComponent("<%=FlowUtility.XM_END_PAY_BALANCE_SHENPI%>")+"','"+rowData.xm_id+"');\"/>&nbsp;&nbsp;"
												+"<input type='button' style='border:0px #ff0000 solid;width:16px;height:16px;cursor:pointer;' class='icon-details' title='查看详情' onclick=\"javascript:viewData('"+rowData.xm_id+"','"+rowData.xm_code+"','"+rowData.xm_name+"');\"/>";
									}
								}
							}
						});
					
						return str;
					}}
				]],
				
				pageSize:10,
				pageList : [10,50,100],  
				pagination:true,
				toolbar:"#tb",
				onLoadSuccess:function(rtJson){
					if(rtJson.rtState== '1')
					{
						$.messager.alert('提示',rtJson.rtMsrg);
					}
				}
			});
		});

		function shenpi(obj,id,xm_code,xm_name){
	    	//flowBusinessUrl获取审批业务记录的URL(可以是个组合参数字符串),moduleFlow获取工作流程的流程名称
	      var flowBusinessUrl="<%=contextPath %>/springViews/xm/tabBalancePayment.jsp?xm_id="+id+"&xm_code="+xm_code+"&xm_name="+xm_name+"&isShow=1";
	      var param="flowBusinessUrl="+encodeURIComponent(flowBusinessUrl)+"&moduleFlow="+encodeURIComponent("<%=FlowUtility.XM_END_PAY_BALANCE_SHENPI%>")+"&businessId="+id+"&isFlowShow=1";
	      $(obj).hide();
	      var hasBalance=0;
	       $.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/xmFinance/hasXMEndPayMain?xm_id='+id,
				async:false,
				success:function(rtjson){
					if(rtjson != '0'){
						hasBalance=1;
					}
				}
			});
			if(hasBalance==0){
			alert("未保存结算数据，请再次进行编辑提交！");
			$(obj).show();
			return;
			}
			
	      $.ajax({
						type:'POST',
						url:'<%=contextPath %>/springMVC/workFlow/toShenpi',
						dataType:"JSON",
						data:param,
						success:function(rtJson){
							if (rtJson.rtState == '0'){
								openDialogResize(rtJson.rtData.url,1000,800);
							}else{
								warningInfo(rtJson.rtMsrg);
							}
							$(obj).show();
						}
					});
	       
	    } 
		
		function addXMPaymentBalance(id,xm_code,xm_name){
			openDialogResize("<%=contextPath %>/springViews/xm/tabBalancePayment.jsp?xm_id="+id+"&xm_code="+xm_code+"&xm_name="+xm_name+"&callback=rtCallBack&isShow=0",1024, 800);
		}
		function viewXMEndPaymentBalance(id,xm_code,xm_name,customer_id,customer,admin_dept_id,admin_dept,xm_business_step,no_material){
			openDialogResize("<%=contextPath %>/springViews/xm/xmEndPayment.jsp?xm_business_step="+xm_business_step+"&xm_id="+id+"&xm_code="+xm_code+"&xm_name="+xm_name+"&customer_id="+customer_id+"&customer="+customer+"&dept_id="+admin_dept_id+"&dept_name="+admin_dept+"&callback=rtCallBack&isShow=0&no_material="+no_material,1024, 800);
		}
		function  viewXMData(id,xm_code,xm_name){
		openDialogResize("<%=contextPath %>/springViews/xm/viewXm.jsp?xm_id="+id+"&xm_code="+xm_code+"&xm_name="+xm_name+"&callback=rtCallBack&isShow=0",1024, 800);
		}
		function  viewData(id,xm_code,xm_name){
			openDialogResize("<%=contextPath %>/springViews/xm/tabBalancePayment.jsp?xm_id="+id+"&xm_code="+xm_code+"&xm_name="+xm_name+"&callback=rtCallBack&isShow=1",1000, 800);
	
		}
		function finishBalance(xm_code){
			var rows=$('#table').datagrid('getSelections');
			$.messager.confirm('确认','您确认该表记录直接结算完成吗？此操作不可逆请谨慎操作！',function(msg){
				if(msg){
			var param = "xm_code="+xm_code;
			$.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/xm/xmFinishBalanceStatus',
				dataType:"JSON",
				data:param,
				success:function(rtJson){
					if (rtJson.rtState == '0'){
						table_reload(rows.length);
					}else{
						warningInfo(rtJson.rtMsrg);
					}
					$(obj).show();
				}
			});
			}
			});
		}
		function rtCallBack(){
			$('#table').datagrid('reload');
		}
		function doSearch()
		{
			var start_time=$('#start_time').datebox('getValue');  
			var end_time=$('#end_time').datebox('getValue');  
			var searchValue = $('#searchValue').val();
			var dept_id = $('#dept_id').combotree('getValue');
			$('#table').datagrid('load',{
				searchValue:searchValue,
				dept_id:dept_id,
				start_time:start_time,
				end_time:end_time
				
			});
		}

	</script>
	<body style="height: 97%">
			<div id="table" data-options="fit:true" ></div>
			<div id="tb" style="padding:3px;height:auto">
				<div align="right">
					<span style="color:red">结算完成请手动点击结算付款里的关闭按钮</span><div style="display:none">开始时间：<input id="start_time" name="start_time" class="easyui-datebox" />&nbsp;&nbsp;&nbsp;结束时间：<input id="end_time" name="end_time" class="easyui-datebox" /></div>
					所属部门：<input  id="dept_id" name="dept_id" class="easyui-combotree">  
					项目编号/名称：<input id="searchValue" name="searchValue" style="width:150px" title="项目编号/名称" placeholder="项目编号/名称 "  />&nbsp;&nbsp; 
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="doSearch();"></a>
				</div>
			</div>
		</body>
</html>
